from bot_api_v1.app.services.business.media_service import MediaError,MediaService,Media_extract_format
from bot_api_v1.app.services.business.xhs_service import XHSService, XHSError , handle_note_info 
from bot_api_v1.app.services.business.script_service import ScriptService, AudioDownloadError, AudioTranscriptionError
from bot_api_v1.app.constants.media_info import MediaType,MediaPlatform
from bot_api_v1.app.services.business.yt_dlp_service import YtDLP_Service_Sync
from bot_api_v1.app.services.business.kuaishou_service import KuaishouService
from bot_api_v1.app.core.logger import logger

import os
import urllib
import json
import ast # 用于解析Python字面量格式的字符串
from ast import Dict

# !! 导入新的同步缓存装饰器 !!
from bot_api_v1.app.core.cache import cache_result_sync
from bot_api_v1.app.core.config import settings
from bot_api_v1.app.tasks.celery_tiktok_service import CeleryTikTokService, TikTokError, InitializationError,VideoFetchError

SPIDER_XHS_LOADED = True 
# @cache_result_sync(
#     expire_seconds=settings.CACHE_EXPIRATION or 1800, 
#     prefix="celery_media_extract_sync", # 统一前缀或分开？暂时统一
#     key_args=['url', 'extract_text', 'platform'] 
# )
# def execute_media_extraction_sync(
#     url: str,
#     extract_text: bool,
#     include_comments: bool, # 这个参数只对 XHS relevant?
#     platform: str,
#     user_id: str, 
#     trace_id: str, 
#     app_id: str   ,
#     root_trace_key: str
# ) -> dict:
#     """
#     [同步执行] 分发器：根据平台调用对应的 Service 的同步方法。
#     应用缓存装饰器。
#     """
#     log_extra = {"request_id": trace_id, "user_id": user_id, "app_id": app_id,"root_trace_key": root_trace_key}
#     logger.info(f"[Sync Logic Dispatcher {trace_id=}] 分发任务: platform={platform}, url={url}", extra=log_extra)

#     try:
#         if platform == MediaPlatform.XIAOHONGSHU:
#             # --- 调用原 XHSService 的同步方法 ---
#             logger.debug("[Sync Logic Dispatcher] Using XHSService.get_note_info_sync_for_celery", extra=log_extra)
#             xhs_service = XHSService() 
#             # !! 假设 XHSService 已有此同步方法 !!
#             result_dict = xhs_service.get_note_info_sync_for_celery( 
#                 url=url, 
#                 extract_text=extract_text, 
#                 user_id_for_points=user_id, 
#                 trace_id=trace_id,
#                 root_trace_key=root_trace_key
#             )
#         elif platform == MediaPlatform.DOUYIN:
#             # --- 调用新的 CeleryTikTokService 的同步方法 ---
#             logger.debug("[Sync Logic Dispatcher] Using CeleryTikTokService.get_video_info_sync", extra=log_extra)
#             # 注意：新 Service 在 __init__ 中初始化，方法结束后在 finally 中清理
#             celery_tiktok_service = CeleryTikTokService() 
#             result_dict = celery_tiktok_service.get_video_info_sync(
#                 url=url, 
#                 extract_text=extract_text,
#                 user_id_for_points=user_id,
#                 trace_id=trace_id,
#                 root_trace_key=root_trace_key
#             )
#         else:
#             raise MediaError(f"不支持的媒体平台: {platform}")

#         logger.info(f"[Sync Logic Dispatcher {trace_id=}] 平台逻辑执行完成. Status: {result_dict.get('status')}", extra=log_extra)
#         return result_dict

#     # 捕获所有可能的服务层异常
#     except (XHSError, TikTokError, MediaError, InitializationError) as e: 
#         error_msg = f"处理失败 ({type(e).__name__}): {str(e)}"
#         logger.error(f"[Sync Logic Dispatcher {trace_id=}] {error_msg}", extra=log_extra, exc_info=True)
#         return {"status": "failed", "error": error_msg, "points_consumed": 0}
#     except Exception as e: 
#         error_msg = f"分发或执行时发生意外错误: {str(e)}"
#         logger.error(f"[Sync Logic Dispatcher {trace_id=}] {error_msg}", exc_info=True, extra=log_extra)
#         return {"status": "failed", "error": f"发生内部错误 ({trace_id})", "exception": str(e), "points_consumed": 0}







# --- 1. 获取基础媒体信息 (适用于 Task A - extract_text=False) ---
@cache_result_sync(
    expire_seconds = 1800,
    prefix="fetch_basic_media",
    key_args=['platform', 'url'] # 缓存不应依赖这些请求上下文变量
)
def fetch_basic_media_info(
    platform: str,
    url: str,
    include_comments: bool, # 注意：可能只对特定平台有意义
    user_id: str,
    trace_id: str,
    app_id: str,
    root_trace_key: str
) -> dict:
    """
    [同步执行] 只获取媒体的基础信息 (元数据, URL等), 不下载文件, 不转写。
    应用缓存。
    """
    log_extra = {"request_id": trace_id, "user_id": user_id, "app_id": app_id, "logic_step": "fetch_basic","root_trace_key": root_trace_key}
    logger.info_to_db(f"[Fetch Basic {trace_id=}] 开始获取基础信息: platform={platform}, url={url}", extra=log_extra)

    base_cost = 10 # 假设基础信息固定成本
    media_data = None

    try:
        if platform == MediaPlatform.XIAOHONGSHU:
            if not SPIDER_XHS_LOADED: raise XHSError("小红书模块未加载")
            xhs_service = XHSService() # 实例化服务
            # 调用 XHS API 获取原始数据 (假设这是阻塞的)
            logger.debug("[Fetch Basic XHS] 调用 xhs_apis.get_note_info...", extra=log_extra)
            success, msg, note_data_raw = xhs_service.xhs_apis.get_note_info(url, xhs_service.cookies_str)
            if not success or not note_data_raw:
                raise XHSError(f"获取小红书笔记API失败--get_note_info--: {msg}")
            # 解析和转换格式 (同步)
            try:
                if note_data_raw['msg'] == '当前笔记暂时无法浏览':
                    raise XHSError(f"获取小红书笔记API失败--get_note_info--: {note_data_raw['msg']}")    
                
                # 添加原始 URL 到原始数据中，方便后续处理
                if 'data' in note_data_raw and 'items' in note_data_raw['data'] and note_data_raw['data']['items']:
                     note_data_raw['data']['items'][0]['url'] = url
                     note_info_parsed = handle_note_info(note_data_raw['data']['items'][0])
                     media_data = xhs_service._convert_note_to_standard_format(note_info_parsed)
                else:
                    raise XHSError("API 返回数据结构不符合预期")
            except Exception as e:
                 logger.error(f"[Fetch Basic XHS] 解析笔记数据失败: {e}", exc_info=True, extra=log_extra)
                 raise XHSError(f"解析笔记数据失败: {str(e)}")

        elif platform == MediaPlatform.DOUYIN:
            tiktok_service = None
            try:
                tiktok_service = CeleryTikTokService() # 实例化服务 (包含初始化和清理)
                extract_text=True
                # 调用内部封装好的同步获取基础信息的方法
                # 这个方法内部处理了在线程池中运行异步代码的逻辑
                media_data = tiktok_service.get_basic_video_info_sync_internal(
                    url=url,
                    extract_text=extract_text,
                    user_id_for_points=user_id,
                    trace_id=trace_id
                )
                if media_data is None: # 检查返回值
                     raise TikTokError("未能获取抖音基础信息 (内部方法返回 None)")
            finally:
                 if tiktok_service:
                     tiktok_service.close_sync() # 确保资源被清理

        elif platform == MediaPlatform.KUAISHOU:     
            ks_service = KuaishouService() 
            media_data = ks_service.get_video_info(trace_id, url, log_extra)
            if media_data is None: # 检查返回值
                raise TikTokError("未能获取KUAISHOU基础信息 (内部方法返回 None)")
        
        elif platform == MediaPlatform.YOUTUBE or platform == MediaPlatform.TIKTOK or platform == MediaPlatform.INSTAGRAM or platform == MediaPlatform.TWITTER or platform == MediaPlatform.BILIBILI:
            yt_dlp_service = YtDLP_Service_Sync()
            media_data = yt_dlp_service.get_basic_info(trace_id, url, log_extra)

            if media_data is None:
                logger.error(f"[Fetch Basic {trace_id=}] 获取{platform}基础信息失败，url={url}", extra=log_extra)
                raise MediaError(f"未能获取B{platform}基础信息 (内部方法返回 None)")
        else:
            raise MediaError(f"不支持的媒体平台: {platform}")

        if media_data is None: # 双重检查
             raise MediaError("未能获取任何媒体数据")

        logger.info_to_db(f"[Fetch Basic {trace_id=}] 基础信息获取成功. Platform={platform}, url={url},basicinfo is {media_data}", extra=log_extra)
        return {"status": "success", "data": media_data, "points_consumed": base_cost}

    # --- 统一错误处理 ---
    except (XHSError, TikTokError, MediaError, InitializationError, VideoFetchError) as e:
        error_msg = f"获取基础信息失败 ({type(e).__name__}): {str(e)}"
        logger.error(f"[Fetch Basic {trace_id=}] {error_msg}", extra=log_extra, exc_info=True)
        return {"status": "failed", "error": error_msg, "points_consumed": 0}
    except Exception as e:
        error_msg = f"获取基础信息时发生意外错误: {str(e)}"
        logger.error(f"[Fetch Basic {trace_id=}] {error_msg}", exc_info=True, extra=log_extra)
        return {"status": "failed", "error": f"发生内部错误 ({trace_id})", "exception": str(e), "points_consumed": 0}


# --- 2. 准备媒体以供转写 (适用于 Task A - extract_text=True) ---
# 注意：这个函数有副作用（下载文件），缓存需要谨慎使用或不使用。
# 如果要缓存，可能只缓存基础信息部分，下载步骤总是执行。
# 这里暂时不加缓存。
def prepare_media_for_transcription(
    platform: str,
    url: str,
    include_comments: bool,
    user_id: str,
    trace_id: str,
    app_id: str,
    root_trace_key: str
) -> dict:
    """
    [同步执行] 获取媒体基础信息，并下载需要转写的音频文件到共享路径。
    不执行转写。
    """
    log_extra = {"request_id": trace_id, "user_id": user_id, "app_id": app_id, "logic_step": "prepare_transcription","root_trace_key": root_trace_key}
    logger.info_to_db(f"[Prepare Transcription {trace_id=}] 开始准备阶段: platform={platform}, url={url}", extra=log_extra)

    audio_path = None
    media_url_to_download = None

    # 1. 获取基础信息
    basic_info_result = fetch_basic_media_info( # 调用上面的函数 (会利用缓存)
        platform=platform,
        url=url,
        include_comments=include_comments,
        user_id=user_id,
        trace_id=trace_id,
        app_id=app_id,
        root_trace_key=root_trace_key
    )

    if basic_info_result.get("status") != "success":
        logger.error(f"[Prepare Transcription {trace_id=}] 获取基础信息失败，准备阶段中止。", extra=log_extra)
        return basic_info_result # 直接返回失败信息

    basic_info_data = basic_info_result.get("data")
    base_points_consumed = basic_info_result.get("points_consumed", 0)

    if not isinstance(basic_info_data, dict): # 健壮性检查
        logger.error(f"[Prepare Transcription {trace_id=}] 获取的基础信息格式不正确 (非字典)", extra=log_extra)
        return {"status": "failed", "error": "内部错误：基础信息格式错误", "points_consumed": 0}

    # 2. 检查是否是视频类型，并获取下载链接
    media_type = basic_info_data.get("type", "").lower() 
    if not media_type :
        media_type = basic_info_data.get("data").get("type").lower()

    if media_type != MediaType.VIDEO.lower():        
        logger.warning(f"[Prepare Transcription {trace_id=}] 媒体类型不是视频 ({media_type}), 无法进行转写准备。", extra=log_extra)
        # 对于非视频，准备阶段可以认为"成功"但没有音频路径，或者标记为不适用？
        # 返回成功，让 Task A 判断 audio_path 是否存在可能更好
        return {
            "status": "success",
            "data": {"basic_info": basic_info_data, "audio_path": None}, # audio_path 为 None
            "points_consumed": base_points_consumed,
            "message": "媒体非视频类型，无需准备转写"
        }

    try:
        # 'transcribe_audio_sync_:{"original_url": "https://www.xiaohongshu.com/explore/67e2b3f900000000030286ce?xsec_token=ABsttmnMANeopanZhB7mwrTWl3izLUb0_nFBSUxqS4EZk=&xsec_source=pc_feed"}'
        media_url_to_download = None
        if platform == MediaPlatform.XIAOHONGSHU or platform == MediaPlatform.BILIBILI or platform == MediaPlatform.KUAISHOU:
            media_url_to_download = basic_info_data.get("media", {}).get("video_url")
        elif platform == MediaPlatform.DOUYIN:
            # 抖音可能优先使用无水印链接或其他下载链接
            media_url_to_download = basic_info_data.get("data").get("downloads") or basic_info_data.get("media_url")
        elif platform == MediaPlatform.TIKTOK or platform == MediaPlatform.YOUTUBE or platform == MediaPlatform.INSTAGRAM:
            media_url_to_download = url # 直接使用 URL
        else:
            media_url_to_download = basic_info_data.get("media_url")  # 通用尝试

        if not media_url_to_download:
            logger.error(f"[Prepare Transcription {trace_id=}] 未能在基础信息中找到有效的视频/音频下载链接", extra=log_extra)
            return {"status": "failed", "error": "无法找到下载链接", "data": {"basic_info": basic_info_data}, "points_consumed": base_points_consumed}


        audio_path = None
        go_cache = False
        # if exists_in_cache('transcribe_audio_sync_',url):
        #     go_cache = True
        #     logger.info_to_db(f"[Prepare Transcription {trace_id=}] ，task B缓存还有效，理论上无需下载音频文件", extra=log_extra)
        # else:# 根据平台获取合适的下载链接
            # 3. 下载音频文件
        logger.info(f"[Prepare Transcription {trace_id=}] 开始下载音频文件从: {media_url_to_download}", extra=log_extra)
        script_service = ScriptService()  # 实例化下载/转写服务
        yt_dlp_service = YtDLP_Service_Sync()

        # 根据平台选择不同的下载方法
        if platform == MediaPlatform.DOUYIN or platform == MediaPlatform.KUAISHOU:
            audio_path= script_service.download_media_sync(url=media_url_to_download,trace_id=trace_id,root_trace_key=root_trace_key)
        elif platform == MediaPlatform.XIAOHONGSHU:
            audio_path, _ = script_service.download_audio_sync(media_url_to_download, trace_id,root_trace_key)
        elif platform == MediaPlatform.BILIBILI :
            audio_path, _  = yt_dlp_service.down_media(trace_id, media_url_to_download,log_extra)
        elif platform == MediaPlatform.YOUTUBE or platform == MediaPlatform.TIKTOK or platform == MediaPlatform.INSTAGRAM:
            audio_path, _ = yt_dlp_service.down_media(trace_id, media_url_to_download,log_extra)
        
        if not audio_path or not os.path.exists(audio_path):  # 检查路径有效性
            raise AudioDownloadError("下载服务未返回有效路径或文件不存在")
        
        logger.info_to_db(f"[Prepare Transcription {trace_id=}] 音频文件下载成功: {audio_path}", extra=log_extra)
        # 准备成功
        return {
            "status": "success",
            "data": {"basic_info": basic_info_data, "audio_path": audio_path, "media_url_to_download":media_url_to_download,"go_cache":go_cache},
            "points_consumed": base_points_consumed  # 准备阶段只计算基础积分
        }

    except AudioDownloadError as e:
        error_msg = f"下载音频文件失败: {str(e)}"
        logger.error(f"[Prepare Transcription {trace_id=}] {error_msg}", extra=log_extra)
        return {"status": "failed", "error": error_msg, "data": {"basic_info": basic_info_data}, "points_consumed": base_points_consumed}
    except Exception as e:
        error_msg = f"下载音频时发生意外错误: {str(e)}"
        logger.error(f"[Prepare Transcription {trace_id=}] {error_msg}", exc_info=True, extra=log_extra)
        return {"status": "failed", "error": f"下载时发生内部错误 ({trace_id})", "exception": str(e), "data": {"basic_info": basic_info_data}, "points_consumed": base_points_consumed}



from typing import Dict, Any, Optional, Tuple, Union, List
media_service = MediaService()
def get_full_info(
    platform: str,
    url: str,
    include_comments: bool, # 注意：可能只对特定平台有意义
    user_id: str,
    trace_id: str,
    app_id: str,
    root_trace_key: str,
    log_extra: Dict[str, Any] = {}):
    # 1: 先获取基本资料
    basic_info = fetch_basic_media_info(platform, url, include_comments, user_id, trace_id, app_id,root_trace_key)
    if basic_info.get("status") != "success":
        logger.error(f"[get_full_info {trace_id=}] 获取基础信息失败，准备阶段中止。", extra=log_extra)

    # 2：获取评论
    # media_service
    # 3：获取KOL

    # 4：统一返回结构
    # 5: 返回




def get_kol_by_url(url:str, trace_id: str, root_trace_key: str, log_extra: Dict[str, Any] = {}):
    logger.info_to_db(f"[Get KOL by URL {trace_id=}] 开始获取KOL信息", extra=log_extra)

    platform = Media_extract_format._identify_platform(url)
    if not platform:
        error_msg = f"无法从URL {url} 判断平台"
        logger.error(f"[Get KOL by URL {trace_id=}] {error_msg}", extra=log_extra)
        return {"status": "failed", "error": error_msg, "platform": "unknown"}
    
    log_extra["platform"] = platform
    logger.info_to_db(f"[Get KOL by URL {trace_id=}] 判断平台为: {platform}", extra=log_extra)
    # 初始化服务
    xhs_service = XHSService()

    try:
        kol_info = None
        if platform == MediaPlatform.XIAOHONGSHU:
            if not SPIDER_XHS_LOADED: raise XHSError("小红书模块未加载")
            urlParse = urllib.parse.urlparse(user_url)
            user_id = urlParse.path.split("/")[-1]

            kol_info = xhs_service.get_user_info( user_id, log_extra=log_extra ) 
        elif platform == MediaPlatform.DOUYIN:
            celery_tiktok_service = CeleryTikTokService()
            try:
                kol_info = celery_tiktok_service.get_user_profile_by_url( url,log_extra=log_extra )
            finally:
                celery_tiktok_service.close_sync()
        else:
            error_msg = f"平台 {platform} 获取KOL信息暂不支持"
            logger.warning(f"[Get KOL by URL {trace_id=}] {error_msg}", extra=log_extra)
            return {"status": "failed", "error": error_msg, "platform": platform}

        if kol_info and kol_info.get("status") == "success":
            logger.info_to_db(f"[Get KOL by URL {trace_id=}] 成功获取KOL信息", extra=log_extra)
            return {"status": "success", "data": kol_info.get("data"), "platform": platform, "points_consumed": kol_info.get("points_consumed", 0)}
        else:
            error_msg = f"获取KOL信息失败或未返回成功状态: {kol_info.get('error') if kol_info else '未知错误'}"
            logger.error(f"[Get KOL by URL {trace_id=}] {error_msg}", extra=log_extra)
            return {"status": "failed", "error": error_msg, "platform": platform, "points_consumed": kol_info.get("points_consumed", 0) if kol_info else 0}

    except (XHSError, TikTokError, MediaError, InitializationError) as e:
        error_msg = f"获取KOL信息时发生服务错误 ({type(e).__name__}): {str(e)}"
        logger.error(f"[Get KOL by URL {trace_id=}] {error_msg}", exc_info=True, extra=log_extra)
        return {"status": "failed", "error": error_msg, "platform": platform}
    except Exception as e:
        error_msg = f"获取KOL信息时发生意外错误: {str(e)}"
        logger.error(f"[Get KOL by URL {trace_id=}] {error_msg}", exc_info=True, extra=log_extra)
        return {"status": "failed", "error": f"内部错误 ({trace_id})", "exception": str(e), "platform": platform}




def get_comments_by_url(url: str, trace_id: str, log_extra: Dict[str, Any] = {}):
    logger.info_to_db(f"[Get Comments by URL {trace_id=}] 开始获取评论信息", extra=log_extra)

    platform = Media_extract_format._identify_platform(url)
    if not platform:
        error_msg = f"无法从URL {url} 判断平台"
        logger.error(f"[Get Comments by URL {trace_id=}] {error_msg}", extra=log_extra)
        return {"status": "failed", "error": error_msg, "platform": "unknown"}
    
    log_extra["platform"] = platform
    logger.info_to_db(f"[Get Comments by URL {trace_id=}] 判断平台为: {platform}", extra=log_extra)

    try:
        comments_data = None
        if platform == MediaPlatform.XIAOHONGSHU:
            if not SPIDER_XHS_LOADED: raise XHSError("小红书模块未加载")
            xhs_service = XHSService()
            comments_data = xhs_service.get_note_all_comment(url, log_extra=log_extra)
        elif platform == MediaPlatform.DOUYIN:
            celery_tiktok_service = CeleryTikTokService()
            try:
                comments_data = celery_tiktok_service.get_all_video_comments(
                    url,
                    platform=platform,
                    log_extra=log_extra
                )
            finally:
                celery_tiktok_service.close_sync()
        # elif platform == MediaPlatform.TIKTOK:
        #     # 类似抖音的处理
        #     celery_tiktok_service = CeleryTikTokService()
        #     try:
        #         comments_data = celery_tiktok_service.get_all_video_comments_sync(
        #             url, 
        #             platform='tiktok',
        #             user_id_for_points=user_id,
        #             trace_id=trace_id,
        #             root_trace_key=root_trace_key
        #         )
        #     finally:
        #         celery_tiktok_service.close_sync()
        else:
            error_msg = f"平台 {platform} 获取评论信息暂不支持"
            logger.warning(f"[Get Comments by URL {trace_id=}] {error_msg}", extra=log_extra)
            return {"status": "failed", "error": error_msg, "platform": platform}

        if comments_data and comments_data.get("status") == "success":
            logger.info_to_db(f"[Get Comments by URL {trace_id=}] 成功获取评论信息", extra=log_extra)
            return {"status": "success", "data": comments_data.get("data"), "platform": platform, "points_consumed": comments_data.get("points_consumed", 0)}
        else:
            error_msg = f"获取评论信息失败或未返回成功状态: {comments_data.get('error') if comments_data else '未知错误'}"
            logger.error(f"[Get Comments by URL {trace_id=}] {error_msg}", extra=log_extra)
            return {"status": "failed", "error": error_msg, "platform": platform, "points_consumed": comments_data.get("points_consumed", 0) if comments_data else 0}

    except (XHSError, TikTokError, MediaError, InitializationError) as e:
        error_msg = f"获取评论信息时发生服务错误 ({type(e).__name__}): {str(e)}"
        logger.error(f"[Get Comments by URL {trace_id=}] {error_msg}", exc_info=True, extra=log_extra)
        return {"status": "failed", "error": error_msg, "platform": platform}
    except Exception as e:
        error_msg = f"获取评论信息时发生意外错误: {str(e)}"
        logger.error(f"[Get Comments by URL {trace_id=}] {error_msg}", exc_info=True, extra=log_extra)
        return {"status": "failed", "error": f"内部错误 ({trace_id})", "exception": str(e), "platform": platform}



def get_user_all_posts(url: str, user_id: str, trace_id: str, app_id: str, root_trace_key: str, log_extra: Dict[str, Any] = {}):
    log_extra.update({
        "request_id": trace_id, 
        "user_id": user_id, 
        "app_id": app_id, 
        "logic_step": "get_user_all_posts",
        "root_trace_key": root_trace_key,
        "target_url": url # 通常 url 是用户主页链接
    })
    logger.info_to_db(f"[Get User All Posts {trace_id=}] 开始获取用户所有帖子信息", extra=log_extra)
    platform = Media_extract_format._identify_platform(url)

    log_extra["platform"] = platform
    logger.info_to_db(f"[Get User All Posts {trace_id=}] 判断平台为: {platform}", extra=log_extra)

    try:
        posts_data = None
        if platform == MediaPlatform.XIAOHONGSHU:
            if not SPIDER_XHS_LOADED: raise XHSError("小红书模块未加载")
            xhs_service = XHSService()
            # 假设 XHSService 有一个同步的 get_user_all_posts_sync 方法
            # 原注释是 get_user_post_note
            posts_data = xhs_service.get_user_post_note(url, log_extra=log_extra)
        elif platform == MediaPlatform.DOUYIN:
            celery_tiktok_service = CeleryTikTokService()
            try:
                # 使用我们之前重构的 get_all_user_posts_sync 方法
                posts_data_list = celery_tiktok_service.get_all_user_posts(
                    user_url_or_sec_id=url, # 传入用户主页URL
                    # max_video_pages, videos_per_page 可以使用默认值或从配置/参数传入
                    platform=platform, # 明确平台
                    log_extra=log_extra
                )
                # CeleryTikTokService 的 get_all_user_posts_sync 返回的是列表，需要包装成期望的字典结构
                posts_data = {"status": "success", "data": posts_data_list, "points_consumed": 0} # 计费逻辑需完善
            finally:
                celery_tiktok_service.close_sync()
        else:
            error_msg = f"平台 {platform} 获取用户所有帖子信息暂不支持"
            logger.warning(f"[Get User All Posts {trace_id=}] {error_msg}", extra=log_extra)
            return {"status": "failed", "error": error_msg, "platform": platform}

        if posts_data and posts_data.get("status") == "success":
            logger.info_to_db(f"[Get User All Posts {trace_id=}] 成功获取用户所有帖子信息", extra=log_extra)
            return {"status": "success", "data": posts_data.get("data"), "platform": platform, "points_consumed": posts_data.get("points_consumed", 0)}
        else:
            error_msg = f"获取用户所有帖子信息失败或未返回成功状态: {posts_data.get('error') if posts_data else '未知错误'}"
            logger.error(f"[Get User All Posts {trace_id=}] {error_msg}", extra=log_extra)
            return {"status": "failed", "error": error_msg, "platform": platform, "points_consumed": posts_data.get("points_consumed", 0) if posts_data else 0}

    except (XHSError, TikTokError, MediaError, InitializationError) as e:
        error_msg = f"获取用户所有帖子信息时发生服务错误 ({type(e).__name__}): {str(e)}"
        logger.error(f"[Get User All Posts {trace_id=}] {error_msg}", exc_info=True, extra=log_extra)
        return {"status": "failed", "error": error_msg, "platform": platform}
    except Exception as e:
        error_msg = f"获取用户所有帖子信息时发生意外错误: {str(e)}"
        logger.error(f"[Get User All Posts {trace_id=}] {error_msg}", exc_info=True, extra=log_extra)
        return {"status": "failed", "error": f"内部错误 ({trace_id})", "exception": str(e), "platform": platform}